package com.maori.languageapp.adapter;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

import android.app.Activity;
import android.graphics.Bitmap;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.ThumbnailUtils;
import android.media.MediaPlayer.OnPreparedListener;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.provider.MediaStore;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.MediaController;
import android.widget.ProgressBar;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.VideoView;

import com.maori.languageapp.R;

public class Languageapp_Curate_AudioView_Activity extends Activity {
	public TextView songName, startTimeField, endTimeField;
	private MediaPlayer mediaPlayer;
	private double startTime = 0;
	private double finalTime = 0;
	private Handler myHandler = new Handler();;
	private int forwardTime = 1000;
	private int backwardTime = 1000;
	private SeekBar seekbar;
	private ImageButton playButton, pauseButton;
	public static int oneTimeOnly = 0;
	VideoView video_view = null;
	String SourceURL;

	@Override
	protected void onCreate(Bundle savedInstanceState) {

		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setFinishOnTouchOutside(false);

		super.onCreate(savedInstanceState);
		setContentView(R.layout.languageapp_audio_preview);

		Bundle extras = getIntent().getExtras();
		if (extras != null) {
			SourceURL = extras.getString("SourceURL");
		}

		// String audio_title ="";

		/*
		 * audio_title = extras.getString("audio_title"); String audio_url =
		 * extras.getString("audio_url");
		 */

		songName = (TextView) findViewById(R.id.textView4);
		startTimeField = (TextView) findViewById(R.id.textView1);
		endTimeField = (TextView) findViewById(R.id.textView2);
		seekbar = (SeekBar) findViewById(R.id.seekBar1);
		playButton = (ImageButton) findViewById(R.id.imageButton1);
		pauseButton = (ImageButton) findViewById(R.id.imageButton2);

		songName.setText("Play Audio");
		mediaPlayer = new MediaPlayer();
		mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
		try {
			mediaPlayer.setDataSource(SourceURL);
			mediaPlayer.prepare();
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (SecurityException e) {
			e.printStackTrace();
		} catch (IllegalStateException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		seekbar.setClickable(false);
		pauseButton.setEnabled(false);

		// requestWindowFeature(Window.FEATURE_NO_TITLE);

		/*
		 * video_view = (VideoView) findViewById(R.id.curate_video_view);
		 * 
		 * final ImageView image_view = (ImageView)
		 * findViewById(R.id.thubanail_view);
		 * 
		 * final ProgressBar prog_view = (ProgressBar)
		 * findViewById(R.id.progressBar1);
		 * 
		 * String videoFile =
		 * "http://langapp.mirzabros.com/lanappvideo/VID_20140507_164454.mp4";
		 * Bitmap thumbnail = ThumbnailUtils.createVideoThumbnail(SourceURL,
		 * MediaStore.Images.Thumbnails.MINI_KIND);
		 * 
		 * image_view.setImageBitmap(thumbnail);
		 * 
		 * final MediaController mediaController = new
		 * MediaController(Languageapp_Curate_AudioView_Activity.this);
		 * 
		 * mediaController.show();
		 * 
		 * mediaController.setAnchorView(video_view);
		 * video_view.setMediaController(mediaController);
		 * 
		 * //http://langapp.mirzabros.com/lanappvideo/VID_20140508_123932.mp4
		 * //video_view.setVideoPath(
		 * "http://langapp.mirzabros.com/lanappvideo/VID_20140507_173943.3gp");
		 * video_view.setVideoURI(Uri.parse(SourceURL));
		 * 
		 * video_view.setZOrderOnTop(true);
		 * 
		 * video_view.requestFocus();
		 * 
		 * image_view.setVisibility(View.VISIBLE);
		 * prog_view.setVisibility(View.VISIBLE);
		 * //video_view.setVisibility(View.GONE);
		 * 
		 * video_view.setOnTouchListener(new View.OnTouchListener() {
		 * 
		 * @Override public boolean onTouch(View v, MotionEvent event) {
		 * mediaController.show();
		 * 
		 * return true;
		 * 
		 * 
		 * } });
		 * 
		 * video_view.setOnPreparedListener(new OnPreparedListener() { public
		 * void onPrepared(MediaPlayer mp) {
		 * 
		 * //image_view.setVisibility(View.GONE);
		 * prog_view.setVisibility(View.GONE);
		 * image_view.setVisibility(View.GONE);
		 * //video_view.setVisibility(View.VISIBLE); video_view.start(); } });
		 */
	}

	public void play(View view) {
		Toast.makeText(getApplicationContext(), "Playing sound",
				Toast.LENGTH_SHORT).show();
		mediaPlayer.start();
		finalTime = mediaPlayer.getDuration();
		startTime = mediaPlayer.getCurrentPosition();
		if (oneTimeOnly == 0) {
			seekbar.setMax((int) finalTime);
			oneTimeOnly = 1;
		}

		endTimeField.setText(String.format(
				"%d min, %d sec",
				TimeUnit.MILLISECONDS.toMinutes((long) finalTime),
				TimeUnit.MILLISECONDS.toSeconds((long) finalTime)
						- TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS
								.toMinutes((long) finalTime))));
		startTimeField.setText(String.format(
				"%d min, %d sec",
				TimeUnit.MILLISECONDS.toMinutes((long) startTime),
				TimeUnit.MILLISECONDS.toSeconds((long) startTime)
						- TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS
								.toMinutes((long) startTime))));
		seekbar.setProgress((int) startTime);
		myHandler.postDelayed(UpdateSongTime, 100);
		pauseButton.setEnabled(true);
		playButton.setEnabled(false);
	}

	private Runnable UpdateSongTime = new Runnable() {
		public void run() {
			startTime = mediaPlayer.getCurrentPosition();
			startTimeField.setText(String.format(
					"%d min, %d sec",
					TimeUnit.MILLISECONDS.toMinutes((long) startTime),
					TimeUnit.MILLISECONDS.toSeconds((long) startTime)
							- TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS
									.toMinutes((long) startTime))));
			seekbar.setProgress((int) startTime);
			myHandler.postDelayed(this, 100);
		}
	};

	public void pause(View view) {
		Toast.makeText(getApplicationContext(), "Pausing sound",
				Toast.LENGTH_SHORT).show();

		mediaPlayer.pause();
		pauseButton.setEnabled(false);
		playButton.setEnabled(true);
	}

	public void forward(View view) {
		int temp = (int) startTime;
		if ((temp + forwardTime) <= finalTime) {
			startTime = startTime + forwardTime;
			mediaPlayer.seekTo((int) startTime);
		} else {
			Toast.makeText(getApplicationContext(),
					"Cannot jump forward 5 seconds", Toast.LENGTH_SHORT).show();
		}

	}

	public void rewind(View view) {
		int temp = (int) startTime;
		if ((temp - backwardTime) > 0) {
			startTime = startTime - backwardTime;
			mediaPlayer.seekTo((int) startTime);
		} else {
			Toast.makeText(getApplicationContext(),
					"Cannot jump backward 5 seconds", Toast.LENGTH_SHORT)
					.show();
		}

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	
}
